package pt.isec.gps1415.g15.drinkanddrive;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainListaBebidas extends Activity {
	private ListView listview;
	private MySQLiteOpenHelper baseDados;
	private ArrayList<Bebida> bebidas;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_lista_bebidas);
		listview= (ListView) findViewById(R.id.listaBebidas);
		
		baseDados=new MySQLiteOpenHelper(getBaseContext());
		
		bebidas=new ArrayList<Bebida>(baseDados.getBebidas());
		
//		String[] stringNomeBebidas=new String[bebidas.size()];
//		for(int i=0;i<bebidas.size();i++)
//		{
//			stringNomeBebidas[i]=bebidas.get(i).getNome();
//		}
		UsersAdapter adapter = new UsersAdapter(this, bebidas);
		// Attach the adapter to a ListView
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new ListaBebidasListener(this,getApplicationContext()));
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.registar_consumo, menu);
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.action_settings:
	        	adicionarBebida();
	            finish();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	void onSelectItemFromList(int id)
	{
		Intent intent=new Intent(this,MainConsultaBebida.class );
		intent.putExtra("ID_BEBIDA", id);
		startActivity(intent);
	}
	private void adicionarBebida()
	{
		Intent intent = new Intent(this, MainAdicionarBebida.class);
		startActivity(intent);
	}
}
class UsersAdapter extends ArrayAdapter<Bebida> {
    public UsersAdapter(Context context, ArrayList<Bebida> users) {
       super(context, 0, users);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // Get the data item for this position
       Bebida user = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.linha_bebida, parent, false);
       }
       // Lookup view for data population
       TextView tvName = (TextView) convertView.findViewById(R.id.nameText);
       TextView tvHome = (TextView) convertView.findViewById(R.id.grauText);
       // Populate the data into the template view using the data object
       tvName.setText(user.getNome());
       tvHome.setText(user.getTeorAlcool()+"%");
       // Return the completed view to render on screen
       return convertView;
   }
}
class ListaBebidasListener implements OnItemClickListener
{
	private Context c;
	private MainListaBebidas main;
	public ListaBebidasListener(MainListaBebidas m,Context context)
	{
		c=context;
		main=m;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Bebida b=(Bebida)parent.getItemAtPosition(position);
		//Toast.makeText(c, "Selecionou: "+b.getNome(), Toast.LENGTH_SHORT).show();
		main.onSelectItemFromList(b.getId());
	}
}
